/*
   This file is part of kleber.
   Copyright 2008 Simon Schmeisser <mail_to_wrt@gmx.de>
   
   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
    
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "KleberPageDisplay.h"
#include "KleberDocument.h"
#include "KleberPage.h"

#include <QtGui/QImage>
//#include <QWidget>
#include <QtGui/QApplication>
#include <QtCore/QList>
#include <QtScript>
#include <QtCore/QTranslator>

#include <QTextStream>
static QTextStream cout(stdout, QIODevice::WriteOnly);

void printHelp()
{
	QString helpText( qApp->translate( "kleber",
				" Kleber uses the following syntax: \n"
				" kleber (OPTION VALUE)* \n"
				" Available Options are: \n"
				" --script\n"
				"\t pass a script as a string, example:\n"
				"\t kleber --script \"print('Hello World');\" \n"
				" \n"
				" --script-file\n"
				"\t load a script from a file, example:\n"
				"\t kleber --script-file \"test.qs\" \n"
				" \n"
				" --before-script \n"
				"\t a piece of script that will be executed before the main script\n"
				"\t use it to pass options to the main script or script file\n"
				"\t example:\n"
				"\t kleber --before-script \"var text = 'Hello World';\" --script \"print(text);\" \n"
				" \n"
				" --after-script \n"
				"\t a piece of script that will be executed after the main script\n"
				"\t use it to clean up after the main script or script file\n"
				"\t example:\n"
				"\t kleber --after-script \"print('Done, cleaning up');\" --script \"print('Hello World');\" \n"
				" "
				" --help\n"
				"\t prints this text\n"
				"\n"
				) );
	cout << helpText;
}

int main (int argc, char *argv[])
{
	QApplication app(argc, argv);
	
	QString locale = QLocale::system().name();
	
	if ( locale == QString("de_DE") ) locale = "de";
	
	QTranslator translator;
	translator.load(QString("kleber_") + locale);
	app.installTranslator(&translator);
	
	
	QString beforeScript, mainScript, afterScript;
	bool validArguments = false;
	
	for (int i = 1; i < argc; i+=(2))
	{
		if ((argv[i][0] != '-') || (argv[i][1] != '-') || (argc <= i+1))
		{
			validArguments = false;
			break;
		}
		if (strcmp(argv[i], "--before-script") == 0)
			beforeScript = QString(argv[i+1]);
		if (strcmp(argv[i], "--script") == 0)
		{
			mainScript = QString(argv[i+1]);
			validArguments = true;
		}
		if (strcmp(argv[i], "--script-file") == 0)
		{
			QFile file(argv[i+1]);
			if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
			{
				cout << app.translate("kleber", "File not found") << "\n";
				return 1;
			}
			
			while (!file.atEnd()) {
				mainScript += file.readLine();
			}
			validArguments = true;
		}
		if (strcmp(argv[i], "--after-script") == 0)
			afterScript = QString(argv[i+1]);
	}
	
	if (!validArguments)
	{
		printHelp();
		return 1;
	}
	
	QScriptEngine engine;
	KleberPage::registerClass(&engine);
	KleberDocument::registerClass(&engine);
	
	engine.evaluate(beforeScript);
	engine.evaluate(mainScript);
	engine.evaluate(afterScript);
	
	cout << "Arguments: \n";
	cout << beforeScript << "\n";
	cout << mainScript << "\n";
	cout << afterScript << "\n";
	/*engine.evaluate(" var document = new Document(); " );
	
	
	//KleberPage *pPage = new KleberPage("00000001.TIF");
	engine.evaluate( "var pPage = new Page('00000001.TIF');" );
	
	//document.appendPage(pPage);
	engine.evaluate( "document.appendPage(pPage);" );

	//pPage = new KleberPage("test.jpg");
	//document.appendPage(pPage);
	
	//pPage = document.takeFirstPage();
	//delete pPage;


	//document.scanAllBarcodes();
	engine.evaluate( " document.scanAllBarcodes(); " );

	engine.evaluate( "document.fileName = 'test1.pdf';" );
	
	engine.evaluate( "document.savePdf();" );
	*/
	
	return 0;// app.exec();
}

